import sys

import pymysql

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class delete_window(QDialog):
    Signal_TwoParameter = pyqtSignal(str)

    def __init__(self, parent=None):
        super(delete_window, self).__init__(parent)
        self.setWindowTitle('删除数据')

        # 在布局中添加部件
        layout = QVBoxLayout(self)
        self.resize(300, 200)

        self.label = QLabel(self)
        self.label.setText('请输入需要删除的记录的车牌号与时间')

        self.tablewidget1 = QTableWidget()
        self.tablewidget1.setRowCount(1)
        self.tablewidget1.setColumnCount(2)
        # 设置列名
        self.tablewidget1.setHorizontalHeaderLabels(["车牌号", "时间"])

        button_clean = QPushButton("清空数据")
        button_clean.clicked.connect(self.clean)

        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(button_clean)
        hbox.addStretch(1)

        layout.addWidget(self.label)
        layout.addWidget(self.tablewidget1)
        # 使用两个button(ok和cancel)分别连接accept()和reject()槽函数
        buttons = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
            Qt.Horizontal, self)
        buttons.accepted.connect(self.delete_handle)
        buttons.rejected.connect(self.reject)
        layout.addLayout(hbox)
        layout.addWidget(buttons)

    def delete_handle(self):
        # 获取表格内容
        id = self.tablewidget1.item(0, 0)
        time = self.tablewidget1.item(0, 1)

        if id == None or time == None:
            QMessageBox.critical(self, '错误', '车牌号与时间为必填项！', QMessageBox.Yes)
            return
        else:
            id = id.text()
            time = time.text()
            if id == '' or time == '':
                QMessageBox.critical(self, '错误', '车牌号与时间为必填项！', QMessageBox.Yes)
                return
            reply = QMessageBox.warning(self, '警告', '删除后无法恢复！', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
            if reply == QMessageBox.Yes:
                print("选择ok")
            conn = pymysql.connect(host='localhost', port=3306, user='root', password="root", db="tab")
            cur = conn.cursor()
            try:
                # 删除的sql语句
                sql = "DELETE FROM tab WHERE Mid=" + "'" + str(id) + "'"+ " and Mdate=" + "'" + str(time) + "'"
                # and "时间=” + id2
                # 打印测试
                print(sql)
                # 执行sql语句，并发送给数据库
                cur.execute(sql)
                conn.commit()

                cur.close()
                conn.close()

                self.Signal_TwoParameter.emit('delete_window_succeed')
                print("删除成功")
            except Exception as e:
                print(e)
                QMessageBox.critical(self, '错误', '数据库中无该数据，请使用读取或查询按钮确定车牌号与时间', QMessageBox.Yes)
                self.Signal_TwoParameter.emit('delete_window_fail')

    def clean(self):
        self.tablewidget1.clearContents()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = delete_window()
    main.show()
    sys.exit(app.exec_())
